﻿/*
JsMockito v1.0.2
Copyright (c) 2009 Chris Leishman

https://github.com/chrisleishman/jsmockito

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, 
       this list of conditions and the following disclaimer.
    
    2. Redistributions in binary form must reproduce the above copyright 
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    3. Neither the name of JsHamcrest nor the names of its contributors may
       be used to endorse or promote products derived from this software
       without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
JsMockito = { version: "1.0.3", _export: ["isMock", "when", "verify", "verifyZeroInteractions", "verifyNoMoreInteractions", "spy"], isMock: function (a) { return typeof a._jsMockitoVerifier != "undefined" }, when: function (a) { return a._jsMockitoStubBuilder() }, verify: function (b, a) { return (a || JsMockito.Verifiers.once()).verify(b) }, verifyZeroInteractions: function () { JsMockito.each(arguments, function (a) { JsMockito.Verifiers.zeroInteractions().verify(a) }) }, verifyNoMoreInteractions: function () { JsMockito.each(arguments, function (a) { JsMockito.Verifiers.noMoreInteractions().verify(a) }) }, spy: function (a) { return (typeof a == "function") ? JsMockito.mockFunction(a) : JsMockito.mock(a) }, contextCaptureFunction: function (b, c) { var a = function () { return a.apply(b, Array.prototype.slice.call(arguments, 0)) }; a.call = function (d) { return a.apply(d, Array.prototype.slice.call(arguments, 1)) }; a.apply = function (e, d) { return c.apply(this, [e, d || []]) }; return a }, matchArray: function (a, b) { if (a.length > b.length) { return false } return !JsMockito.any(a, function (d, c) { return !d.matches(b[c]) }) }, toMatcher: function (a) { return JsHamcrest.isMatcher(a) ? a : JsHamcrest.Matchers.equalTo(a) }, mapToMatchers: function (a) { return JsMockito.map(a, function (b) { return JsMockito.toMatcher(b) }) }, each: function (d, c) { if (d.length == undefined) { for (var b in d) { c(d[b], b) } } else { for (var a = 0; a < d.length; a++) { c(d[a], a) } } }, map: function (c, b) { var a = []; JsMockito.each(c, function (e, d) { var f = b(e, d); if (f != null) { a.push(f) } }); return a }, grep: function (c, b) { var a = []; JsMockito.each(c, function (e, d) { if (b(e, d)) { a.push(e) } }); return a }, find: function (c, b) { for (var a = 0; a < c.length; a++) { if (b(c[a], a)) { return c[a] } } return undefined }, any: function (b, a) { return (this.find(b, a) != undefined) }, extend: function (b, a) { for (var c in a) { b[c] = a[c] } return b }, verifier: function (a, b) { JsMockito.Verifiers[a] = function () { JsMockito.Verifier.apply(this, arguments) }; JsMockito.Verifiers[a].prototype = new JsMockito.Verifier; JsMockito.Verifiers[a].prototype.constructor = JsMockito.Verifiers[a]; JsMockito.extend(JsMockito.Verifiers[a].prototype, b) } }; JsMockito.mockFunction = function (e, b) { if (typeof e == "function") { b = e; e = undefined } e = e || "func"; b = b || function () { }; var d = []; var f = []; var c = function () { var h = [this]; h.push.apply(h, arguments); f.push({ args: h }); var g = JsMockito.find(d, function (k) { return JsMockito.matchArray(k[0], h) }); if (g == undefined) { return b.apply(this, arguments) } var j = g[1]; if (j.length == 0) { return undefined } var i = j[0]; if (j.length > 1) { j.shift() } return i.apply(this, arguments) }; c.prototype = b.prototype; c._jsMockitoStubBuilder = function (g) { var g = g || JsHamcrest.Matchers.anything(); return a(g, function (h) { var i = [h, []]; d.push(i); return { then: function () { i[1].push.apply(i[1], arguments); return this }, thenReturn: function () { this.then.apply(this, JsMockito.map(arguments, function (j) { return function () { return j } })) }, thenThrow: function (j) { this.then.apply(this, JsMockito.map(arguments, function (k) { return function () { throw k } })) } } }) }; c._jsMockitoVerifier = function (g, h) { var h = h || JsHamcrest.Matchers.anything(); return a(h, function (i) { return g(e, f, i, i[0] != h) }) }; c._jsMockitoMockFunctions = function () { return [c] }; return c; function a(h, g) { return JsMockito.contextCaptureFunction(h, function (k, j) { var i = JsMockito.mapToMatchers([k].concat(j || [])); return g(i) }) } }; JsMockito._export.push("mockFunction"); JsMockito.mock = function (e) { var h = {}; if (typeof e != "function") { h = e; e = function () { }; e.prototype = h; e.prototype.constructor = e } var f = function () { }; f.prototype = new e; f.prototype.constructor = f; var g = new f(); var j = {}; var a = {}; var b = JsHamcrest.Matchers.sameAs(g); var d = function (i) { var m; if (h[i] != undefined) { m = function () { var n = (this == g) ? h : this; return h[i].apply(n, arguments) } } g[i] = JsMockito.mockFunction("obj." + i, m); j[i] = g[i]._jsMockitoStubBuilder; a[i] = g[i]._jsMockitoVerifier }; for (var l in g) { if (l != "constructor") { d(l) } } for (var k in JsMockito.nativeTypes) { if (g instanceof JsMockito.nativeTypes[k].type) { for (var c = 0; c < JsMockito.nativeTypes[k].methods.length; ++c) { d(JsMockito.nativeTypes[k].methods[c]) } } } g._jsMockitoStubBuilder = function () { var m = new f(); for (var i in j) { m[i] = j[i].call(this, b) } return m }; g._jsMockitoVerifier = function (i) { var m = new f(); for (var n in a) { m[n] = a[n].call(this, i, b) } return m }; g._jsMockitoMockFunctions = function () { return JsMockito.map(g, function (i) { return JsMockito.isMock(i) ? i : null }) }; return g }; JsMockito._export.push("mock"); JsMockito.nativeTypes = { Array: { type: Array, methods: ["concat", "join", "pop", "push", "reverse", "shift", "slice", "sort", "splice", "toString", "unshift", "valueOf"] }, Boolean: { type: Boolean, methods: ["toString", "valueOf"] }, Date: { type: Date, methods: ["getDate", "getDay", "getFullYear", "getHours", "getMilliseconds", "getMinutes", "getMonth", "getSeconds", "getTime", "getTimezoneOffset", "getUTCDate", "getUTCDay", "getUTCMonth", "getUTCFullYear", "getUTCHours", "getUTCMinutes", "getUTCSeconds", "getUTCMilliseconds", "getYear", "setDate", "setFullYear", "setHours", "setMilliseconds", "setMinutes", "setMonth", "setSeconds", "setTime", "setUTCDate", "setUTCMonth", "setUTCFullYear", "setUTCHours", "setUTCMinutes", "setUTCSeconds", "setUTCMilliseconds", "setYear", "toDateString", "toGMTString", "toLocaleDateString", "toLocaleTimeString", "toLocaleString", "toString", "toTimeString", "toUTCString", "valueOf"] }, Number: { type: Number, methods: ["toExponential", "toFixed", "toLocaleString", "toPrecision", "toString", "valueOf"] }, String: { type: String, methods: ["anchor", "big", "blink", "bold", "charAt", "charCodeAt", "concat", "fixed", "fontcolor", "fontsize", "indexOf", "italics", "lastIndexOf", "link", "match", "replace", "search", "slice", "small", "split", "strike", "sub", "substr", "substring", "sup", "toLowerCase", "toUpperCase", "valueOf"] }, RegExp: { type: RegExp, methods: ["compile", "exec", "test"]} }; JsMockito.Verifiers = { _export: ["never", "zeroInteractions", "noMoreInteractions", "times", "once"], never: function () { return new JsMockito.Verifiers.Times(0) }, zeroInteractions: function () { return new JsMockito.Verifiers.ZeroInteractions() }, noMoreInteractions: function () { return new JsMockito.Verifiers.NoMoreInteractions() }, times: function (a) { return new JsMockito.Verifiers.Times(a) }, once: function () { return new JsMockito.Verifiers.Times(1) } }; JsMockito.Verifier = function () { this.init.apply(this, arguments) }; JsMockito.Verifier.prototype = { init: function () { }, verify: function (a) { var b = this; return a._jsMockitoVerifier(function () { b.verifyInteractions.apply(b, arguments) }) }, verifyInteractions: function (c, d, a, b) { }, updateVerifiedInteractions: function (a) { JsMockito.each(a, function (b) { b.verified = true }) }, buildDescription: function (d, e, a, b) { var c = new JsHamcrest.Description(); c.append(d + ": " + e + "("); JsMockito.each(a.slice(1), function (g, f) { if (f > 0) { c.append(", ") } c.append("<"); g.describeTo(c); c.append(">") }); c.append(")"); if (b) { c.append(", 'this' being "); a[0].describeTo(c) } return c } }; JsMockito.verifier("Times", { init: function (a) { this.wanted = a }, verifyInteractions: function (f, e, a, b) { var g = JsMockito.grep(e, function (h) { return JsMockito.matchArray(a, h.args) }); if (g.length == this.wanted) { this.updateVerifiedInteractions(g); return } var d; if (g.length == 0) { d = "Wanted but not invoked" } else { if (this.wanted == 0) { d = "Never wanted but invoked" } else { if (this.wanted == 1) { d = "Wanted 1 invocation but got " + g.length } else { d = "Wanted " + this.wanted + " invocations but got " + g.length } } } var c = this.buildDescription(d, f, a, b); throw c.get() } }); JsMockito.verifier("ZeroInteractions", { verify: function (a) { var b = JsMockito.Verifiers.never(); JsMockito.each(a._jsMockitoMockFunctions(), function (c) { b.verify(c)() }) } }); JsMockito.verifier("NoMoreInteractions", { verify: function (a) { var b = this; JsMockito.each(a._jsMockitoMockFunctions(), function (c) { JsMockito.Verifier.prototype.verify.call(b, c)() }) }, verifyInteractions: function (e, d, a, b) { var f = JsMockito.grep(d, function (g) { return g.verified != true }); if (f.length == 0) { return } var c = this.buildDescription("No interactions wanted, but " + f.length + " remains", e, a, b); throw c.get() } }); JsMockito.Integration = { importTo: function (b) { for (var a = JsMockito._export.length; a > 0; --a) { var c = JsMockito._export[a - 1]; b[c] = JsMockito[c] } for (var a = JsMockito.Verifiers._export.length; a > 0; --a) { var c = JsMockito.Verifiers._export[a - 1]; b[c] = JsMockito.Verifiers[c] } }, screwunit: function () { JsMockito.Integration.importTo(Screw.Matchers) }, JsTestDriver: function () { JsMockito.Integration.importTo(window) }, JsUnitTest: function () { JsMockito.Integration.importTo(JsUnitTest.Unit.Testcase.prototype) }, YUITest: function () { JsMockito.Integration.importTo(window) }, QUnit: function () { JsMockito.Integration.importTo(window) }, jsUnity: function () { JsMockito.Integration.importTo(jsUnity.env.defaultScope) }, jSpec: function () { JsMockito.Integration.importTo(jSpec.defaultContext) } };
